<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/13
 * Time: 9:21
 */
namespace Admin\Controller;

class FieldProductController extends CommonController{

    function _initialize()
    {
        parent::_initialize();
        $this->_fieldproduct = M('FieldProduct');
    }
    public function index()
    {
        $this->field = $this->_fieldproduct->select();
        $this->display();
    }

    //添加
    public function add()
    {
        if(IS_POST){
            if($this->_fieldproduct->where(array('field_name'=>$_POST['field_name']))->find()){
                $this->error('字段名称重复，请重新添加！');
            }
            switch($_POST['field_type']){
                case 1:
                    $typechar='int(10)';
                    break;
                case 2:
                    $typechar='VARCHAR(255)';
                    break;
                case 3:
                    $typechar='text';
                    break;
            }

            if($this->_fieldproduct->add($_POST)){
                $sql = "ALTER TABLE `".C('DB_PREFIX')."custom_product` ADD `".$_POST['field_name']."` $typechar;";
                M()->execute($sql);
                $this->success('添加模型成功！', U(MODULE_NAME.'/FieldProduct/index'));
            }else{
                $this->error('添加模型失败！');
            }
        }else{
            $this->display();
        }
    }

    public function edit()
    {
        if(IS_POST){
            $id = $_GET['id'];
            switch($_POST['field_type']){
                case 1:
                    $typechar='int(10)';
                    break;
                case 2:
                    $typechar='VARCHAR(255)';
                    break;
                case 3:
                    $typechar='text';
                    break;
            }
            $res = $this->_fieldproduct->where(array('field_name'=>$_POST['field_name'],'id'=>array('NEQ',$id)))->find();
            if($res){
                $this->error('修改的字段名称出现重复，请重新修改！');
            }
            //先获取数据库中老的field_name 用于修改数据字段
            $das = $this->_fieldproduct->find($id);
            if($this->_fieldproduct->where(array('id'=>$id))->save($_POST)){
                $sql = "ALTER TABLE `".C('DB_PREFIX')."custom_product` change `".$das['field_name']."` `".$_POST['field_name']."` ".$typechar.";";
                M()->execute($sql);
                //echo M()->getLastSql();
                $this->success('修改模型成功！', U(MODULE_NAME.'/FieldProduct/index'));
            }else{
                $this->error('修改模型失败！');
            }

        }else{
            $id = I('id');
            $this->field = $this->_fieldproduct->find($id);
            $this->display();
        }
    }

    //删除文章
    public function delete($id){
        //先获取数据库中老的field_name 用于修改数据字段
        $das = $this->_fieldproduct->find($id);
        $res = $this->_fieldproduct->where(array('id'=>$id))->delete();
        if($res){
            $sql = "alter table  `".C('DB_PREFIX')."custom_product`  DROP COLUMN `".$das['field_name']."`;";
            M()->execute($sql);
            $data =[
                'status'=>1,
                'info'=>'删除模型成功！',
            ];
        }else{
            $data = [
                'status'=>0,
                'info'=>'删除模型失败，请稍后再试！',
            ];
        }
        $this->ajaxReturn($data);
    }

}